package model.simulation;

import com.github.crab2died.annotation.ExcelField;
import model.AbstractDepartment;
import model.AbstractModel;
import model.dynamic.DyDepartment;

import java.util.ArrayList;
import java.util.List;

//D级单位
abstract public class SiDepartment extends DyDepartment
{

	// 该单位所有编制链表
	public final ArrayList<SiModel> siAuthorizedModels = new ArrayList<>();
	public ArrayList<SiModel> siActualModels = new ArrayList<>();
	public ArrayList<SiModel> notTotalMatchModels = new ArrayList<>();
	
	@ExcelField(title = "补入人数", order = 14)
	protected int addNumber=0;

	public SiDepartment()
	{
	}

	public void calculateStatisticData()
	{
		if (hasSubDepartment)
		{
			notMatchAuthorizedModels.clear();
			notMatchActualModels.clear();
			matchActualModels.clear();
			for (AbstractDepartment subDepartment : subDepartments)
			{
				subDepartment.calculateStatisticData();
				notMatchAuthorizedModels.addAll(subDepartment.notMatchAuthorizedModels);
				notMatchActualModels.addAll(subDepartment.notMatchActualModels);
				matchActualModels.addAll(
						((DyDepartment) subDepartment).matchActualModels);
			}
		}
		int temp= getActualNumber();
		super.calculateStatisticData();
		addNumber= getActualNumber()-temp;
	}

	@Override
	public void calculateAdditionalData()
	{
	}

	public void changInNextYear()
	{
		notMatchAuthorizedModels.clear();
		notMatchAuthorizedModels.addAll(siAuthorizedModels);

		notMatchActualModels.clear();
		notMatchActualModels.addAll(siActualModels);
		matchActualModels.clear();
		for (AbstractModel model : notMatchActualModels)
			model.number = 1;
		for (AbstractModel model : notMatchAuthorizedModels)
			model.number = 1;
	}

	public void initMatch()
	{
		for (AbstractModel authorizedModel : notMatchAuthorizedModels)
		{
			for (int i = 0; i < authorizedModel.number; i++)
				siAuthorizedModels.add(new SiModel((SiModel) authorizedModel, false));
		}

		for (AbstractModel actualModel : notMatchActualModels)
			siActualModels.add((SiModel) actualModel);

		// 未匹配人员链表
		List<SiModel> matchModels = new ArrayList<>();
		List<SiModel> siAuthorizedModels = new ArrayList<>(this.siAuthorizedModels);
		List<SiModel> siActualModels = new ArrayList<>(this.siActualModels);

		for (SiModel actualModel : siActualModels)
		{
			for (SiModel authorizedModel : siAuthorizedModels)
			{
				if (actualModel.isTotalMatch(authorizedModel))// 是否全匹配
				{
					actualModel.matchAuthorizedModel(authorizedModel);
					matchModels.add(actualModel);
					siAuthorizedModels.remove(authorizedModel);
					break;
				}
			}
		}
		siActualModels.removeAll(matchModels);
		notTotalMatchModels.addAll(siActualModels);
		matchModels.clear();

		for (SiModel actualModel : siActualModels)
		{
			for (SiModel authorizedModel : siAuthorizedModels)
			{
				if (actualModel.isQuarterProfessionMatch(authorizedModel))// 是否岗位和专业匹配
				{
					actualModel.matchAuthorizedModel(authorizedModel);
					matchModels.add(actualModel);
					siAuthorizedModels.remove(authorizedModel);
					break;
				}
			}
		}
		siActualModels.removeAll(matchModels);
		matchModels.clear();

		for (SiModel actualModel : siActualModels)
		{
			for (SiModel authorizedModel : siAuthorizedModels)
			{
				if (actualModel.isRankProfessionMatch(authorizedModel))// 是否等级和专业匹配
				{
					actualModel.matchAuthorizedModel(authorizedModel);
					matchModels.add(actualModel);
					siAuthorizedModels.remove(authorizedModel);
					break;
				}
			}
		}

		siActualModels.removeAll(matchModels);
		matchModels.clear();

		for (SiModel actualModel : siActualModels)
		{
			for (SiModel authorizedModel : siAuthorizedModels)
			{
				if (actualModel.isProfessionMatch(authorizedModel))// 是否专业匹配
				{
					actualModel.matchAuthorizedModel(authorizedModel);
					matchModels.add(actualModel);
					siAuthorizedModels.remove(authorizedModel);
					break;
				}
			}
		}
		siActualModels.removeAll(matchModels);
		matchModels.clear();
		for (SiModel actualModel : siActualModels)
		{
			for (SiModel authorizedModel : siAuthorizedModels)
			{
				if (actualModel.isRankMatch(authorizedModel))// 是否等级匹配
				{
					actualModel.matchAuthorizedModel(authorizedModel);
					matchModels.add(actualModel);
					siAuthorizedModels.remove(authorizedModel);
					break;
				}
			}
		}
		siActualModels.removeAll(matchModels);

		int index = 0;
		for (SiModel authorizedModel : siAuthorizedModels)
			if (index < siActualModels.size())
				siActualModels.get(index++).matchAuthorizedModel(authorizedModel);
	}

	public int getAddNumber()
	{
		return addNumber;
	}

	public void setAddNumber(int addNumber)
	{
		this.addNumber = addNumber;
	}
}
